/**
 * 功能描述 列表
 */

define(function (require, exports, module){
	var EditGrid = require("editgrid");
	var enmu=require("enums");
	var store =require("store");
	var activeUser = store.get("user");
	var onCreditForm = require("../component/onCreditForm");
	var roles = activeUser.roles;
	var instance = {
		initComponent:function (params,widget){
			var totalGrid = new EditGrid({
				parentNode:".J-total",
				autoRender : false,
				model:{
					id:"total",
					isInitPageBar: false,
					allowAdd: false,
					columns:[{
						name: "roomTotal",
						label: "住宿费合计",
						format: function(value, data) {
    		            	if(value){
    		            		return parseFloat(value).toFixed(2);	
    		            	}else{
    		            		return 0;
    		            	}
			            },
					},{
						name:"trafficTotal",
						label:"大交通往返费合计",
						format: function(value, data) {
    		            	if(value){
    		            		return parseFloat(value).toFixed(2);	
    		            	}else{
    		            		return 0;
    		            	}
			            },
					},{
						name:"otherTotal",
						label:"其他费用合计",
						format: function(value, data) {
    		            	if(value){
    		            		return parseFloat(value).toFixed(2);	
    		            	}else{
    		            		return 0;
    		            	}
			            },
					},{
						name:"allTotal",
						label:"费用总金额",
						format: function(value, data) {
    		            	if(value){
    		            		return parseFloat(value).toFixed(2);	
    		            	}else{
    		            		return 0;
    		            	}
			            },
					},{
						name:"depositTotal",
						label:"预收款合计",
						format: function(value, data) {
    		            	if(value){
    		            		return parseFloat(value).toFixed(2);	
    		            	}else{
    		            		return 0;
    		            	}
			            },
					},{
						name:"deductionChargesTotal",
						label:"权证抵扣合计",
						format: function(value, data) {
    		            	if(value){
    		            		return parseFloat(value).toFixed(2);	
    		            	}else{
    		            		return 0;
    		            	}
			            },
					},{
						name:"receivableTotal",
						label:"应收金额合计",
						format: function(value, data) {
    		            	if(!isNaN(value)){
    		            		return parseFloat(value).toFixed(2);	
    		            	}else{
    		            		return 0;
    		            	}
			            },
					},{
						name:"receiptsTotal",
						label:"实收金额合计",
						editor:{
							type:"text",
							onChange:function(plugin,index,rowData){
								rowData.receiptsTotal=parseFloat(plugin.getValue());
								totalGrid.update(index,rowData);
							}
						},
						format: function(value, data) {
    		            	if(value){
    		            		return parseFloat(value).toFixed(2);	
    		            	}else{
    		            		return 0;
    		            	}
			            },
					},{
						name:"paymentMethod",
						label:"结算方式",
						format : function(value, row) {
							var arr = [];
							if(value.length>0){
								for(var i=0;i<value.length;i++){
									arr.push(value[i].value);
								}
							}
							
							return arr.join(",");
		    			},
						editor:{
							type:"select",
							multi : true,
							onEdit:function(plugin,index,rowData){
								var arr1=[{key:"Cash",value:"现金"},{key:"WipeCard",value:"刷卡"},
								          {key:"Transfer",value:"转账"},{key:"OnCredit",value:"挂账"}];
								plugin.setData(arr1);
								var paymentMethod = rowData.paymentMethod;
								var vals = [];
								for(var i = 0 ; i<paymentMethod.length; i++){
									vals[i] = paymentMethod[i].key;
								}
								plugin.setValue(vals);
							},
							onChange:function(plugin,index,rowData){
								var paymentMethod = [];
								var paymentMethodValue = plugin.getValue();
								for(var i = 0; i < paymentMethodValue.length; i++){
									paymentMethod[i] = plugin.getData(paymentMethodValue[i]);
								}
//								for ( var i=0;i < paymentMethod.length;i++) {
//									if(paymentMethod[i].key=="OnCredit"){
//										onCreditForm.initComponent(params, widget);
//										widget.get("onCreditForm").destroy();
//										widget.show([".J-onCreditForm"]);
//										break;
//									}else{
//										widget.hide([".J-onCreditForm"])
//									}
//								}
//								if(paymentMethod.length==0){
//									widget.hide([".J-onCreditForm"]);
//									widget.get("onCreditForm").destroy(); 
//								}
								
								var flag = false;
								for ( var i=0;i < paymentMethod.length;i++) {
									if(paymentMethod[i].key=="OnCredit"){
										flag = true;
								}
								if(flag){
									widget.show([".J-onCreditForm"]);
									onCreditForm.initComponent(params, widget);
								}else{
									widget.hide([".J-onCreditForm"])
								}
								
								
								if(paymentMethod.length==0){
									widget.hide([".J-onCreditForm"])
									widget.get("onCreditForm").destroy(); 
								}
								
								rowData.paymentMethod = paymentMethod;
								totalGrid.update(index,rowData);
							}
						}
					 }
					},{
						name:"description",
						label:"备注",
						editor:{
							type:"text",
							onChange:function(plugin,index,rowData){
								rowData.description=plugin.getValue();
								totalGrid.update(index,rowData);
							}
						}
					}]
				}
			});
			widget.set("totalGrid",totalGrid);
		}
	};
	module.exports = instance;
});
